package com.cndw.kungfu.dao;

import java.util.List;

import com.cndw.dao.jade.annotation.DAO;
import com.cndw.dao.jade.annotation.SQL;
import com.cndw.dao.jade.annotation.SQLParam;
import com.cndw.dao.jade.annotation.shard.ShardConfig;
import com.cndw.kungfu.core.PlayerIdShardStrategy;
import com.cndw.kungfu.domain.Event;
import com.cndw.kungfu.domain.LogCost;
import com.cndw.kungfu.domain.LogGot;
import com.cndw.kungfu.domain.LogPlayer;

@DAO
public interface LogDao {

	public static final String Table_Cost = "kf_cost_log";
	public static final String Field_Cost = "id,playerId,stype,gold,goldTicke,vipScore,goodsId,num,eventId,addTime";

	public static final String Table_Data = "kf_log_player:SKEY";
	public static final String Field_Data = "id,playerId,objectId,eventId,level,exp,gold,goldTicke,asset,params,ip,addTime";

	public static final String table_Got = "kf_log_gold";
	public static final String filed_Got = "id,playerId,gold,eventId,addTime";

	/**
	 * 插入消费日志
	 * 
	 * @param logCost
	 */
	@SQL("INSERT INTO " + Table_Cost + " (" + Field_Cost
			+ ") VALUES(:p.id,:p.playerId,:p.stype,:p.gold,:p.goldTicke,:p.vipScore,:p.goodsId,:p.num,:p.eventId,:p.addTime) ")
	public void saveLogCost(@SQLParam("p") LogCost logCost);

	@SQL("select goodsId from " + Table_Cost + " where playerId = :1 and goodsId>0  and stype = 1 group by goodsId order by max(id) desc limit 12 ")
	public List<Integer> selectBuyIds(long playerId);

	@SQL("select addTime from " + Table_Cost + " where playerId = :1 and goodsId=:2 order by id desc limit 1 ")
	public int selectLastBuyTime(long playerId, int goodsId);

	@ShardConfig(strategy = PlayerIdShardStrategy.class, value = "p.playerId")
	@SQL("INSERT INTO " + Table_Data + " (" + Field_Data
			+ ") VALUES(:p.id,:p.playerId,:p.objectId,:p.eventId,:p.level,:p.exp,:p.gold,:p.goldTicke,:p.asset,:p.params,:p.ip,:p.addTime) ")
	public void saveLogPlayer(@SQLParam("p") List<LogPlayer> logPlayers);

	@ShardConfig(strategy = PlayerIdShardStrategy.class, value = "playerId")
	@SQL("SELECT "
			+ Field_Data
			+ " FROM "
			+ Table_Data
			+ " where 1=1 #if(:playerId>0){and playerId=:playerId } #if(:objectId>0){and objectId=:objectId } #if(:eventId>0){and eventId=:eventId } #if(:param){and params like :param } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } #if(:ip){and ip = :ip } order by id desc, addTime desc limit :offset,:limit")
	public List<LogPlayer> getLogs(@SQLParam("playerId") long playerId, @SQLParam("objectId") int objectId, @SQLParam("eventId") int eventId,
			@SQLParam("param") String param, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime, @SQLParam("offset") int offset,
			@SQLParam("limit") int limit,@SQLParam("ip") String ip);

	@ShardConfig(strategy = PlayerIdShardStrategy.class, value = "playerId")
	@SQL("SELECT COUNT(*) FROM "
			+ Table_Data
			+ " where 1=1 #if(:playerId>0){and playerId=:playerId } #if(:objectId>0){and objectId=:objectId } #if(:eventId>0){and eventId=:eventId } #if(:param){and params like :param } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } #if(:ip){and ip =  :ip } ")
	public int getLogCounts(@SQLParam("playerId") long playerId, @SQLParam("objectId") int objectId, @SQLParam("eventId") int eventId,
			@SQLParam("param") String param, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime,@SQLParam("ip") String ip);

	@SQL("INSERT INTO " + table_Got + " (" + filed_Got + ") VALUES(0,:p.playerId,:p.gold,:p.eventId,:p.addTime) ")
	public void saveLogGot(@SQLParam("p") LogGot logGot);

	@SQL("select sum(gold) from " + Table_Cost + " where playerId=:1 and gold>0  and addTime>=:2 and addTime <= :3 and eventId in (:4) ")
	public int selectCostSum(long playerId, int beginTime, int endTime, int[] ids);

	@SQL("delete from kf_log_player0 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize ")
	public int clearLog0(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player1 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog1(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player2 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog2(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player3 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog3(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player4 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog4(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player5 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog5(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player6 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog6(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player7 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog7(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player8 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog8(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	@SQL("delete from kf_log_player9 where addTime <= UNIX_TIMESTAMP()-30*86400 and eventId in (:ids) limit :lsize")
	public int clearLog9(@SQLParam("ids") int[] eventIds,@SQLParam("lsize") int lsize);

	
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player0  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType0(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player1  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType1(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player2  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType2(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player3  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType3(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player4  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType4(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player5  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType5(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player6  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType6(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player7  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType7(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player8  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType8(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	@SQL("SELECT "+ Field_Data+ " FROM kf_log_player9  where 1=1  #if(:eventId>0){and eventId=:eventId } #if(:beginTime>0){and addTime>:beginTime } #if(:endTime>0){and addTime<:endTime } order by id desc, addTime desc ")
	public List<LogPlayer> getAllByType9(@SQLParam("eventId") int eventId, @SQLParam("beginTime") int beginTime, @SQLParam("endTime") int endTime);
	
	
	@SQL("SELECT * from  kf_player_mail  where 1=1 and playerId =:1 and title=:2 and addTime>=:3  ")
	public Event getEvent(long uid,String title,int addTime);
	
	@SQL("delete from  kf_player_mail where playerId=:1 and id=:2 ")
	public void delEvent(long uid,int id);
	
}
